//https://zh.wikipedia.org/wiki/AV%E5%A5%B3%E5%84%AA%E5%88%97%E8%A1%A8

var utils = require('utils');
var system = require('system');
var keyword = system.args[4];
var url  = system.args[5];
var searchCount = 0;
var is_started = false;
url = url || 'http://192.168.31.208:4711/';
keyword = keyword || 'siro';

console.log(url + ' started.');
var casper = require('casper').create({
        "pageSettings" : {
            loadImages: false,
            loadPlugins: false,
            javascriptEnabled: true,
            localToRemoteUrlAccessEnabled: false,
            resourceTimeout: 5000
        },
        clientScripts:[  
            'jquery.js'  
        ],
        timeout: 50000,
        logLevel: "error",//日志等级
        verbose: true,    // 记录日志到控制台
        onError: function(casper, msg, backtrace) {
        	
        },
        onLoadError: function(casper, url, status) {
            this.echo(url + "===>loadError " + status);
            casper.exit();
        },
        httpStatusHandlers: {
            404: function(self, resource) {
                self.echo(resource.url + ' not found STATUS_ERROR (404)', 'ERROR');
                casper.exit();
            },
            500: function(self, resource) {
                self.echo(resource.url + ' gave an error STATUS_ERROR (500)', 'ERROR');
                casper.exit();
            },
            200: function(self, resource) {
                this.echo('new location is ' + resource.url, 'COMMENT');
            }
        }
});

casper.on('error', function(msg,backtrace) {
	this.echo(msg, 'ERROR');
	//this.echo(this.getHTML());
	casper.exit();
});

casper.on('timeout', function () {
    this.echo(this.getCurrentUrl().trim() + "===>timeout", 'ERROR');
    casper.exit();
});

casper.on('resourceTimeout', function () {
    this.echo(this.getCurrentUrl().trim() + "===>resourceTimeout", 'ERROR');
    casper.exit();
});

casper.start(url).waitForText("在这里输入你的密码", function() {
	this.echo('Login form, fill password and click', 'INFO');
    this.fill('form', {
        'p': ''
    }, true);
});

casper.then(function() {
	this.echo('Click search menu', 'INFO');
	this.click('.tabs_three td:nth-child(7) a');
});

casper.waitForSelector('#search', function() {
	this.echo('Search form, fill keyword and submit', 'INFO');
	this.fill('form#search', {
        'tosearch': keyword,
        'method':  'kademlia', //'kademlia',
    }, true);
});

casper.then(function() {
    searchCount = getSearchCount();
	this.echo(searchCount + ' result found, start wait...', 'INFO');	
});

casper.wait(20000, function() {
    this.click('a#research');
    this.echo("I've waited for 20 seconds. click research", 'INFO');
});

casper.waitForText('搜索结果', function(){
	searchCount = getSearchCount();
	this.echo(searchCount + ' result found', 'INFO');
    if (searchCount > 0) {
        this.download(this.getCurrentUrl(), 'emule_all/search/' + keyword + '.html');
    }
});

casper.run(function () {
    this.echo(url + ' finished', 'GREEN_BAR').exit();
});

function getSearchCount() {
	var searchCount = casper.evaluate(function() {
	    var search = $('td.search-line-left');
	    return search.length;
	});

	return searchCount;
}
